<template>
  <a-table v-bind="props"
           :pagination="mergedPagination"
           :row-selection="mergedRowSelection"
           v-on="$attrs">
    <template v-for="(_, name) in $slots" #[name]="slotData" :key="name">
      <slot :name="name" v-bind="slotData || {}"></slot>
    </template>
  </a-table>
</template>

<script setup lang="ts">
import {computed} from 'vue';
import type {TableRowSelection} from "ant-design-vue/es/table/interface";
import {getRandomString} from "@/utils/utils.ts";

const props = defineProps<{
  rowKey?: string;
  rowSelection?: TableRowSelection;
  pagination?: any; // 或更精确的类型
}>();
const mergedPagination = computed(() => {
  if (props.pagination === false) return false;
  return {
    position: ['bottomCenter'],
    size: 'middle',
    showSizeChanger: true,
    showTotal: (total: number) => `共 ${total} 条`,
    defaultCurrent: 1,
    defaultPageSize: 10,
    hideOnSinglePage: true,
    ...props.pagination,
  };
});
const mergedRowSelection = computed(() => {
  if(!props.rowSelection) return null;
  return {
    ...props.rowSelection,
    getCheckboxProps:(_record:any)=>{
        return {
          name: `table-name-${props.rowKey ? _record[props.rowKey] : getRandomString()}`,
        }
    },
  };
});
</script>